package com.thememaker.util.packager;

import com.alibaba.fastjson.JSON;
import com.thememake.Constants;
import com.thememake.common.PsdMaker;
import com.thememake.common.base.Strings;
import com.thememake.entity.ThemeFileInfo;
import com.thememaker.lockstyle.bean.CheckOut;
import com.thememaker.lockstyle.bean.Lockscreen;
import com.thememaker.lockstyle.bean.Var;
import com.thememaker.lockstyle.bean.ui.*;
import com.thememaker.lockstyle.control.LayoutComponentUtils;

import java.util.LinkedHashMap;
import java.util.SortedMap;
import java.util.function.Consumer;


public class LockScreenCheckUtils {

    private Lockscreen lockscreen;
    private ThemeFileInfo themeFileInfo;
    private CodeCheck codeCheck;
    private boolean canShowErrorNotification = true;

    public LockScreenCheckUtils(Lockscreen lockscreen, ThemeFileInfo themeFileInfo){
        this.lockscreen = lockscreen;
        this.themeFileInfo = themeFileInfo;
    }

    public void checkLockscreen(Consumer<CheckOut> callBack){
        LinkedHashMap<String,BaseView> baseViewLinkedHashMap = lockscreen.getLockScreenViewMap();

        String themeType = firstCharToUpperCase(themeFileInfo.getThemeType().toLowerCase());
        String psdMakerClassName = String.format("com.thememaker.util.packager.%s",
                themeType + CodeCheck.class.getSimpleName());
            try {
                codeCheck = (CodeCheck) Class.forName(psdMakerClassName).newInstance();
            } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        if (codeCheck != null){
            System.err.println("codeCheck 不为空");
            codeCheck.checkLockscreen(lockscreen,callBack);
            checkLinkedView(baseViewLinkedHashMap,callBack);
            codeCheck.showErrorNotification(canShowErrorNotification,callBack);
        }
    }

    private String firstCharToUpperCase(String themeType){
        char[] chars = themeType.toCharArray();
        if (chars[0] >= 'a' && chars[0] <= 'z') {
            chars[0] = (char)(chars[0] - 32);
        }
        return new String(chars);
    }

    private void checkLinkedView(LinkedHashMap<String,BaseView> baseViewLinkedHashMap,Consumer<CheckOut> callBack){
        if(baseViewLinkedHashMap.size()==0){
            callBack.accept(new CheckOut(0,codeCheck.Error_Lockscreen_File_Damaged ));
        }else {
            baseViewLinkedHashMap.entrySet().forEach(stringBaseViewEntry -> {
                if (!LayoutComponentUtils.head_judge || !LayoutComponentUtils.content_Determine){
                    return ;
                }
                BaseView baseView = stringBaseViewEntry.getValue();
                System.err.println(stringBaseViewEntry.getKey() + " >>>> " + JSON.toJSONString(stringBaseViewEntry.getValue()));
                codeCheck.checkVisibility(baseView,callBack);
                if (baseView instanceof Group) {
                    checkLinkedView(((Group)baseView).getGroupViewMap(),callBack);

                } else if (baseView instanceof Image) {
                    codeCheck.checkImage((Image) baseView,callBack);
                } else if (baseView instanceof Slider) {
                    Slider slider = (Slider) baseView;
                    codeCheck.checkSlider(slider,callBack);
                } else if (baseView instanceof Wallpaper) {

                } else if (baseView instanceof Button) {
                    Button button = new Button();
                    codeCheck.checkButton(button,callBack);
                } else if (baseView instanceof Var){
                    Var var= (Var) baseView;
                    if (CodeCheck.Notification.equals(var.getName())){
                        canShowErrorNotification = false;
                    }
                    codeCheck.checkVar(var,callBack);
                }
            });
        }

    }


}
